home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 114_01.zip / ED9.BDS < prev    next >
Text File  |  1993-06-01  |  4KB  |  242 lines

  1. /* Screen editor:  general utilities
  2.  *           BDS C version
  3.  *
  4.  * Source:  ed9.cc
  5.  * Version: May 15, 1981.
  6.  */
  7.  
  8. /* define global constants and variables */
  9.  
  10. #include ed.h
  11. #include bdscio.h
  12. #include ed1.ccc
  13. #include edext.cc
  14.  
  15. /* return: is first token in args a number ? */
  16. /* return value of number in *val            */
  17.  
  18. number(args,val) char *args; int *val;
  19. {
  20. char c;
  21.     c=*args++;
  22.     if ((c<'0')||(c>'9')) {
  23.         return(NO);
  24.     }
  25.     *val=c-'0';
  26.     while (c=*args++) {
  27.         if ((c<'0')||(c>'9')) {
  28.             break;
  29.         }
  30.         *val=(*val*10)+c-'0';
  31.     }
  32.     return(YES);
  33. }
  34.  
  35. /* convert character buffer to numeric */
  36.  
  37. ctoi(buf,index) char *buf; int index;
  38. {
  39. int k;
  40.     while ( (buf[index]==' ')||
  41.         (buf[index]==TAB) ) {
  42.         index++;
  43.     }
  44.     k=0;
  45.     while ((buf[index]>='0')&&(buf[index]<='9')) {
  46.         k=(k*10)+buf[index]-'0';
  47.         index++;
  48.     }
  49.     return(k);
  50. }
  51.  
  52.  
  53. /* put decimal integer n in field width >= w.
  54.  * left justify the number in the field.
  55.  */
  56.  
  57. putdec(n,w) int n,w;
  58. {
  59. char chars[10];
  60. int i,nd;
  61.     nd=itoc(n,chars,10);
  62.     i=0;
  63.     while (i<nd) {
  64.         syscout(chars[i++]);
  65.     }
  66.     i=nd;
  67.     while (i++<w) {
  68.         syscout(' ');
  69.     }
  70. }
  71.  
  72. /* convert integer n to character string in str */
  73.  
  74. itoc(n,str,size) int n; char *str; int size;
  75. {
  76. int absval;
  77. int len;
  78. int i,j,k;
  79.     absval=abs(n);
  80.     /* generate digits */
  81.     str[0]=0;
  82.     i=1;
  83.     while (i<size) {
  84.         str[i++]=(absval%10)+'0';
  85.         absval=absval/10;
  86.         if (absval==0) {
  87.             break;
  88.         }
  89.     }
  90.     /* generate sign */
  91.     if ((i<size)&&(n<0)) {
  92.         str[i++]='-';
  93.     }
  94.     len=i-1;
  95.     /* reverse sign, digits */
  96.     i--;
  97.     j=0;
  98.     while (j<i) {
  99.         k=str[i];
  100.         str[i]=str[j];
  101.         str[j]=k;
  102.         i--;
  103.         j++;
  104.     }
  105.     return(len);
  106. }
  107.  
  108. /* system error routine */
  109.  
  110. syserr(s) char *s;
  111. {
  112.     pmtmess("system error: ",s);
  113. }
  114.  
  115. /* user error routine */
  116.  
  117. error(s) char *s;
  118. {
  119.     pmtmess("error: ",s);
  120. }
  121.  
  122. /* disk error routine */
  123.  
  124. diskerr(s) char *s;
  125. {
  126.     pmtmess("disk error: ",s);
  127. }
  128.  
  129. /* read the next line of the file into
  130.  * the buffer of size n that p points to.
  131.  * Successive calls to readline() read the file
  132.  * from front to back.
  133.  */
  134.  
  135. readline(file,p,n) int file; char *p; int n;
  136. {
  137. int c;
  138. int k;
  139.     k=0;
  140.     while (1) {
  141.         c=sysrdch(file);
  142.         if (c==ERR) {
  143.             return(ERR);
  144.         }
  145.         if (c==EOF) {
  146.             /* ignore line without CR */
  147.             return (EOF);
  148.         }
  149.         if (c==CR) {
  150.             return(k);
  151.         }
  152.         if (k<n) {
  153.             /* move char to buffer */
  154.             *p++=c;
  155.         }
  156.         /* always bump count */
  157.         k++;
  158.     }
  159. }
  160.  
  161. /* push (same as write) line to end of file.
  162.  * line is in the buffer of size n that p points to.
  163.  * lines written by this routine may be read by
  164.  * either readline() or popline().
  165.  */
  166.  
  167. pushline(file,p,n) int file; char *p; int n;
  168. {
  169.     /* write all but trailing CR */
  170.     while ((n--)>0) {
  171.         if (syspshch(*p++,file)==ERR) {
  172.             return(ERR);
  173.         }
  174.     }
  175.     /* write trailing CR */
  176.     return(syspshch(CR,file));
  177. }
  178.  
  179. /* pop a line from the back of the file.
  180.  * the line should have been pushed using pushline().
  181.  */
  182.  
  183. popline(file,p,n) int file; char *p; int n;
  184. {
  185. int c;
  186. int k, kmax, t;
  187.     /* first char must be CR */
  188.     c=syspopch(file);
  189.     if (c==EOF) {
  190.         /* at START of file */
  191.         return(EOF);
  192.     }
  193.     if (c==CR) {
  194.         /* put into buffer */
  195.         *p++=CR;
  196.         k=1;
  197.     }
  198.     else {
  199.         syserr("popline: missing CR");
  200.         return(ERR);
  201.     }
  202.     /* pop line into buffer in reverse order */
  203.     while (1) {
  204.         c=syspopch(file);
  205.         if (c==ERR) {
  206.             return(ERR);
  207.         }
  208.         if (c==EOF) {
  209.             break;
  210.         }
  211.         if (c==CR) {
  212.             /* this ends ANOTHER line */
  213.             /* push it back           */
  214.             if (syspshch(CR,file)==ERR) {
  215.                 return(ERR);
  216.             }
  217.             break;
  218.         }
  219.         /* non-special case */
  220.         if (k<n) {
  221.             /* put into buffer */
  222.             *p++=c;
  223.         }
  224.         /* always bump count */
  225.         k++;
  226.     }
  227.     /* remember if we truncated the line */
  228.     kmax=k;
  229.     /* reverse the buffer */
  230.     k=min(k,n-1);
  231.     t=0;
  232.     while (k>t) {
  233.         /* swap p[t], p[k] */
  234.         c=p[k];
  235.         p[k]=p[t];
  236.         p[t]=c;
  237.         k--;
  238.         t++;
  239.     }
  240.     return(kmax);
  241. }
  242.